package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.dto.configinfo.*;
import cn.juque.systemservice.api.provider.IConfigInfoProvider;
import cn.juque.systemservice.entity.ConfigInfo;
import cn.juque.systemservice.service.IConfigInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>系统参数表</li>
 * </ul>
 * @date 2022-10-27 23:30:46
 **/
@Slf4j
@Service("configInfoProvider")
@HandlerAppException
public class ConfigInfoProviderImpl implements IConfigInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IConfigInfoService configInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<ConfigInfoListDTO>> list(ConfigInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        ConfigInfo configInfo = new ConfigInfo();
        BeanUtil.copyProperties(searchDTO, configInfo);
        List<ConfigInfo> infoList;
        if (null == pageInfo) {
            infoList = this.configInfoService.listInfoWithoutPage(configInfo);
        } else {
            infoList = this.configInfoService.listInfoWithPage(pageInfo, configInfo);
        }
        List<ConfigInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, ConfigInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return ConfigInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<ConfigInfoDetailDTO> detail(String id) {
        ConfigInfo configInfo = this.configInfoService.detail(id);
        ConfigInfoDetailDTO detailDTO = new ConfigInfoDetailDTO();
        BeanUtil.copyProperties(configInfo, detailDTO);
        return new BaseResponseDTO<>(MessageEnum.OK, detailDTO);
    }

    /**
     * 新增
     *
     * @param addDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> add(ConfigInfoAddDTO addDTO) {
        this.validatorUtil.fastValidate(addDTO);
        this.baseOperatorThreadLocal.set(addDTO.getBaseOperator());
        ConfigInfo configInfo = new ConfigInfo();
        BeanUtil.copyProperties(addDTO, configInfo);
        this.configInfoService.saveInfo(configInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新
     *
     * @param updateDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> update(ConfigInfoUpdateDTO updateDTO) {
        this.validatorUtil.fastValidate(updateDTO);
        this.baseOperatorThreadLocal.set(updateDTO.getBaseOperator());
        ConfigInfo configInfo = new ConfigInfo();
        BeanUtil.copyProperties(updateDTO, configInfo);
        this.configInfoService.updateInfo(configInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> delete(ConfigInfoDeleteDTO req) {
        List<String> infoIdList = req.getIdList();
        this.configInfoService.deleteInfo(infoIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }
}
